package com.neca.auction.pages;

import java.util.List;

import javax.swing.text.html.HTMLDocument.HTMLReader.HiddenAction;

import org.apache.tapestry.SelectModel;
import org.apache.tapestry.annotations.ApplicationState;
import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.corelib.components.TextField;
import org.apache.tapestry.ioc.Messages;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.util.EnumSelectModel;

import com.neca.auction.dbb.DBBroker;
import com.neca.auction.model.Category;
import com.neca.auction.model.CurrentUser;
import com.neca.auction.model.Offer;
import com.neca.auction.model.Product;
import com.neca.auction.model.User;


public class AllProducts {
	
	@Inject
	private Messages messages;
	@ApplicationState
	private CurrentUser currentUser;
	@Persist
	private double highestOffer;
	
	private double myHighestOffer;
	@Persist
	private Product product;
	@Component
	private Form licitationForm;
	@Component
	private Form err;
	@Persist
	private Category category;
	
	int submitor=0;
	
	
	
	
	public List<Product> getAllProducts(){
		DBBroker dbbroker=DBBroker.getDbbroker();
		if(submitor>0){
		//System.out.println(category+"aaaaaaaaaaaaa");
		//System.out.println(category.ordinal());
			if(category!=null){
		return dbbroker.getProductsInCategory(category.ordinal());
			}
			else{
				return dbbroker.getProducts();
			}
		}
		submitor=submitor+1;
		return dbbroker.getProducts();
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}
	
	public String getDesShort(){
		try{
		String shortDes=product.getDescription().substring(0, 50);
		return shortDes+"....";
		}catch(Exception e){
			return product.getDescription();
		}
	}

	public double getHighestOffer() {
		System.out.println(product.getName());
		DBBroker dbbroker=DBBroker.getDbbroker();
		double price= dbbroker.getHighestOffer(product);
		if(product.getPrice()<price){
			return price;
		}
		else{
			return product.getPrice();
		}
		
	}
	
	public SelectModel getCategories(){
		return new EnumSelectModel(Category.class, messages);
	}

	public void setHighestOffer(double highestOffer) {
		this.highestOffer = highestOffer;
	}
	
	

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public double getMyHighestOffer() {
		return myHighestOffer;
	}

	public void setMyHighestOffer(double myHighestOffer) {
		this.myHighestOffer = myHighestOffer;
	}
	
	void onSubmitFromLicitationForm(){
		System.out.println("Form was submitted!!!");
		
		
		
	}
	void onValidate(int productID){
		
		
	}
	
	void onSuccess(int productID){
		
		DBBroker dbbroker=DBBroker.getDbbroker();
		Product product=dbbroker.getProduct(productID);
		if(myHighestOffer>product.getPrice()){
		if(myHighestOffer<dbbroker.getHighestOffer(product)){
			Product p=dbbroker.getProduct(productID);
			category = p.getCategory();
			setMyHighestOffer(0);
			//licitationForm.recordError(messages.get("passwor-match"));
			err.clearErrors();
			err.recordError(messages.get("err-price"));
			return; //AllProducts.class;
			
		}
		}else{
			//licitationForm.recordError(messages.get("passwor-match"));
			Product p=dbbroker.getProduct(productID);
			category = p.getCategory();
			err.clearErrors();
			err.recordError(messages.get("err-price"));
			return; //AllProducts.class;
		}
		
		User u = new User("","",currentUser.getUserName(),"","","",0);
		User user=dbbroker.getUserByUserName(u);
		dbbroker.addOffer(new Offer(productID,user.getUserID() , myHighestOffer));
		Product p=dbbroker.getProduct(productID);
		category = p.getCategory();
		System.out.println("ubaceno u bazu!!!!!!!!!!!!!");
		err.clearErrors();
		//return AllProducts.class;
		
		/*System.out.println(product.getName());
		DBBroker dbbroker=new DBBroker();
		User u = new User("","",currentUser.getUserName(),"","","",0);
		User user=dbbroker.getUserByUserName(u);
		dbbroker.addOffer(new Offer(productID,user.getUserID() , myHighestOffer));
		
		return AllProducts.class;*/
	}
	
	void onFailure(){
		System.out.println("onFailure.");
		
	}
	
	
	
	
	
	

}
